$().ready(function () {

	var qrCode = $("#qrCode").val();
	$(".qrcodeImgUpImg").attr('src',qrCode);

	$("#content_sn").html($("#examIntro").val())
	$("#trainingForm_sn").html($("#trainingExplain").val())

	$('#content_sn').summernote({
		height: '300px',
		lang: 'zh-CN',
		toolbar: [
			['style', ['bold', 'italic', 'underline', 'clear']],
			['font', ['strikethrough', 'superscript', 'subscript']],
			['fontsize', ['fontsize']],
			['fontname', ['fontname']],
			['color', ['color']],
			['para', ['ul', 'ol', 'paragraph']],
			['height', ['height']],
			['table', ['table']],
			['insert', ['link', 'picture']],
			['view', ['fullscreen', 'codeview', 'help']]
		],
		callbacks: {
			onImageUpload: function (files, editor, $editable) {
				sendFiles(files,'#content_sn');
			},
			onPaste: function (ne) {
				var bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
				ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
				setTimeout(function () {
					document.execCommand("insertText", false, bufferText);
				}, 10);
			}
		}
	});

	$('#trainingForm_sn').summernote({
		height: '300px',
		lang: 'zh-CN',
		toolbar: [
			['style', ['bold', 'italic', 'underline', 'clear']],
			['font', ['strikethrough', 'superscript', 'subscript']],
			['fontsize', ['fontsize']],
			['fontname', ['fontname']],
			['color', ['color']],
			['para', ['ul', 'ol', 'paragraph']],
			['height', ['height']],
			['table', ['table']],
			['insert', ['link', 'picture']],
			['view', ['fullscreen', 'codeview', 'help']]
		],
		callbacks: {
			onImageUpload: function (files, editor, $editable) {
				sendFiles(files,'#trainingForm_sn');
			},
			onPaste: function (ne) {
				var bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
				ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
				setTimeout(function () {
					document.execCommand("insertText", false, bufferText);
				}, 10);
			}
		}
	});

	//编辑器新增的ajax上传图片函数
	function sendFiles(files, editor, $editable) {
		var formData = new FormData();
		formData.append("file", files[0]);
		$.ajax({
			data : formData,
			type : "POST",
			url : "/common/sysFile/upload",    // 图片上传出来的url，返回的是图片上传后的路径，http格式
			cache : false,
			contentType : false,
			processData : false,
			dataType : "json",
			success: function(data) {//data是返回的hash,key之类的值，key是定义的文件名
				$(editor).summernote('insertImage',data.data);
			},
			error:function(data){
				//console.log("data",data)
				alert("上传失败");
			}
		});
	}

	if(!$("#trainingPdf").val()){
		$("#brand").html('')
	}
})

layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#QRcodeAdd',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'images/*', //只筛选图片
		exts:'jpg|png|gif|bmp|jpeg',
		size:10240,
		multiple:true,
		number:5,
		done: function (r) {
			if( r.code == 0 ){
				layer.msg('上传成功!');
				let str = '';

				str = `
					<div class="closeBtn">
						<img class="upImg" src="${r.data}" width="95"/>
						<span class="iconclose"><img src="../../../img/close.png"></span>
					</div>
				  `;

				$("#QRcodeImg").html(str);
				$("#qrCode").val( r.data );

				$(".iconclose").click(function (){
					$("#qrCode").val('');
					$("#QRcodeImg").html('');
				})

			}
			else{
				layer.msg('上传失败!');
			}

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});


$("#registrationStartDate").val($("#registrationStartDate").val().substring(0,10));
$("#registrationEndDate").val($("#registrationEndDate").val().substring(0,10));
$("#trainingStartDate").val($("#trainingStartDate").val().substring(0,10));
$("#trainingEndDate").val($("#trainingEndDate").val().substring(0,10));

layui.use('laydate', function(){
	var laydate = layui.laydate;

	laydate.render({
		elem: '#times'
		//设置开始日期、日期日期的 input 选择器
		//数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
		,range: ['#registrationStartDate', '#registrationEndDate']
	});

	laydate.render({
		elem: '#timesOut'
		//设置开始日期、日期日期的 input 选择器
		//数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
		,range: ['#trainingStartDate', '#trainingEndDate']
	});

	laydate.render({
		elem: '#classMeetingDate'
		,type: 'datetime'
	});

	laydate.render({
		elem: '#graduationDate'
		,type: 'datetime'
	});

})

layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#btnAdd',
		url: '/api/oss/upload1',
		accept: 'pdf', //只允许上传图片
		acceptMime: 'pdf/*', //只筛选图片
		exts:'pdf',
		size:10240,
		multiple:true,
		number:5,
		done: function (r) {
			if( r.code == 0 ){
				layer.msg('上传成功!');
				let str = '', url = [];
				let arr = [] , fileName = [];

				$("#brand").find(".closeBtn").each(function (k,v){
					arr.push({'url': $(v).find('a').attr('href'),name: $(v).find('p').text() })
				})

				arr.push({url:r.data.url,name:r.data.name});
				arr.forEach(function (v,k){
					url.push(v.url)
					fileName.push(v.name)
					str += `
						<div class="closeBtn">
							<a href="${v.url}" target="_blank">
								<img class="upImg" src="../../../img/pdf.png" width="95"/>
								<p>${v.name}</p>
							</a>
							<span class="iconclose"><img src="../../../img/close.png"></span>
						</div>
					  `;
				})

				$("#brand").html(str);
				$("#trainingPdf").val( url.join(',') );
				$("#fileName").val(fileName.join(','));

				$(".iconclose").click(function (){
					let arrImg = [];
						fileName = [];

					$(this).parent('.closeBtn').remove();
					$("#brand").find('.closeBtn').each(function (k,v){
						arrImg.push($(v).find('a').attr('href'));
						fileName.push($(v).find('p').text())
					})

					$("#trainingPdf").val( arrImg.join(',') );
					$("#fileName").val(fileName.join(','));
				})

			}
			else{
				layer.msg('上传失败!');
			}

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});

let fileName = $("#fileList").val() , fileStr = '';

fileName = JSON.parse(fileName);
$(".iconclose").click(function (){
	$("#brand").html('')
	$("#trainingPdf").val('')
})

fileName.forEach(function (v,k){
	v.idx = k;
	fileStr += `
		<div class="closeBtn">
			<a href="${v.trainingPdf}" target="_blank">
				<img class="upImg" src="../../../img/pdf.png" width="95"/>
				<p>${v.fileName}</p>
			</a>
			<span class="iconcloseImg"><img src="../../../img/close.png"></span>
		</div>
	  `;
})

$("#brand").html(fileStr);

$(".iconcloseImg").click(function (){
	arrImg = [];
	fileName = [];
	$(this).parent('.closeBtn').remove();
	$("#brand").find('.closeBtn').each(function (k,v){
		fileName.push($(v).find('a').attr('href'));
		arrImg.push($(v).find('p').text());
	})
	$("#trainingPdf").val( fileName.join(',') );
	$("#fileName").val(arrImg.join(','));
})

$.validator.setDefaults({
	submitHandler: function () {
		update();
	}
});


function update() {

	if($("#suggestId").val()){
		$("#trainingAddress").val($("#suggestId").val())
	}

	$("#examIntro").val($('#content_sn').summernote('code'));
	$("#trainingExplain").val($('#trainingForm_sn').summernote('code'));

	$.ajax({
		cache: true,
		type: "POST",
		url: "/jzweb/examRegistration/updateExamRegistration",
		data: $('#signupForm').serialize(),// 你的formid
		async: false,
		error: function (request) {
			parent.layer.alert("Connection error");
		},
		success: function (data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
				parent.reLoad();
			} else {
				parent.layer.alert(data.msg)
			}
		}
	});

}

$("#trainingForm").change(function (){

	if( $(this).val() == '1'){
		$("#address").hide()
	}else{
		$("#address").show()
	}
})

if( $("#training").val() == '2'){
	$("#address").show()
}

$("#trainingForm").val($("#training").val())

layui.use('upload', function () {
	var upload = layui.upload;
	upload.render({
		elem: '#columnPictureAdd',
		url: '/common/sysFile/upload',
		accept: 'images', //只允许上传图片
		acceptMime: 'images/*', //只筛选图片
		exts:'jpg|png|gif|bmp|jpeg',
		size:10240,
		multiple:true,
		number:5,
		done: function (r) {
			if( r.code == 0 ){
				layer.msg('上传成功!');
				let str = '';

				str = `
					<div class="closeBtn">
						<img class="upImg" src="${r.data}" width="95"/>
						<span class="iconcloseImg"><img src="../../../img/close.png"></span>
					</div>
				  `;

				$("#columnPictureImg").html(str);
				$("#columnPicture").val( r.data );

				$(".iconclose").click(function (){
					$("#columnPicture").val('');
					$("#columnPictureImg").html('');
				})

			}
			else{
				layer.msg('上传失败!');
			}

		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});